<?php

namespace App\Http\Controllers\Admin;

use App\Models\SaleUser;
use App\Models\SaleUserLogin;
use App\Models\SaleLog;
use App\Models\ShopSms;
use App\Services\CommonService;
use Illuminate\Http\Request;
use App\Models\ShopUser;

class SaleUserController extends BaseController
// class SaleUserController
{
    public function saleExtendList(Request $request)
    {
        $saleUser=SaleUser::where('channel_name','<>','')->select('sale_user_id','channel_name')->get();

        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', obToArray($saleUser));
    }
    //申请审批列表/所有只能本公司审批
    public function saleApplyList(Request $request)
    {
        $input = $request->input();

        $where = [];
        // $where[] = ['is_check', CommonService::SHOP_DISABLE];
        if (!empty($input['mobile'])) {
            $where[] = ['mobile', 'like', '%' . trim($input['mobile']) . '%'];
        }
        $page = isset($input['page']) ? $input['page'] : 1;
        $pageSize = isset($input['pageSize']) ? $input['pageSize'] : CommonService::PAGESIZE;

        $data = SaleUser::GetAllSaleUser($pageSize, $where);

        $data = formatPage($data);
        $data['shop']=ShopUser::where('status',1)->get()->toArray();
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
    }
    //审批通过
    public function checkPass(Request $request)
    {
        $input = $request->input();
        $sale_user = SaleUser::GetSaleUser(array('id' => $input['sale_user_id']));
        if($sale_user['is_check'] == CommonService::SHOP_ENABLE){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'状态有误，无法审核');
        }

        $data = [];
        $data['is_check'] = CommonService::SHOP_ENABLE; //审核通过
        $data['is_show'] = CommonService::SHOP_ENABLE; //启用账号
        $data['check_remark'] = '审核通过-'.$input['check_remark']; //审核备注
        //test
        $data['check_user_id'] = $this->users['id']; //审核人员ID
        // $data['check_user_id'] = 3; //审核人员ID
        $data['check_time'] = date('Y-m-d H:i:s'); //审核时间

        $res = SaleUser::where('id',$input['sale_user_id'])->update($data);

        $data2 = [];
        $data2['is_show'] = CommonService::SHOP_ENABLE; //启用账号

        $res = SaleUserLogin::where('sale_user_id',$sale_user['sale_user_id'])->where('role',CommonService::SALE_ALL)->update($data2);

        if($res){

            //添加操作记录
            $data = [];
            $data['sale_user_id'] = $input['sale_user_id'];
            $data['user_id'] = $this->users['id'];
            $data['content'] = '分销商'.$sale_user['faname'].'审核通过';
            $data['remark'] = '';
            SaleLog::addLog($data);

            $saleUserLogin = SaleUserLogin::GetSaleUser(array('sale_user_id' => $input['sale_user_id']));
            //发短信
            // ShopSms::saleRegisterPass($saleUserLogin['username'],$saleUserLogin['mobile']);

            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '审核通过执行成功', $data);
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'操作有误，请重新尝试');
        }
    }

    //设定归属商家
    public function selectShop(Request $request)
    {
        $input = $request->input();
        $sale_user = SaleUser::GetSaleUser(array('id' => $input['sale_user_id']));
        if(!$sale_user){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'不存在');
        }


        $data2['shop_user_id'] = $input['member_id']; //启用账号

        $res = SaleUser::where('sale_user_id',$sale_user['sale_user_id'])->update($data2);

        if($res){
            //添加操作记录
            $data = [];
            $data['sale_user_id'] = $input['sale_user_id'];
            $data['user_id'] = $this->users['id'];
            $data['content'] = '分销商'.$sale_user['faname'].'设定归属商家'.$data2['shop_user_id'] ;
            $data['remark'] = '';
            SaleLog::addLog($data);

//            $saleUserLogin = SaleUserLogin::GetSaleUser(array('sale_user_id' => $input['sale_user_id']));
            //发短信
            // ShopSms::saleRegisterPass($saleUserLogin['username'],$saleUserLogin['mobile']);

            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '更改商家归属成功', $data);
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'操作有误，请重新尝试');
        }
    }

    //审批拒绝
    public function checkRefuse(Request $request)
    {
        $input = $request->input();

        $sale_user = SaleUser::GetSaleUser(array('id' => $input['sale_user_id']));

        if($sale_user['is_check'] == CommonService::SHOP_ENABLE){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'状态有误，无法审核');
        }

        $data = [
            'is_check'=>CommonService::SHOP_CHECK,
            'is_show'=>CommonService::SHOP_DISABLE,
            'check_remark'=>'审核拒绝-'.$input['check_remark'],
            'check_user_id'=>$this->users['id'],
            'check_time'=>date('Y-m-d H:i:s'),
        ];

        $res = (new SaleUser)::where('id',$input['sale_user_id'])->update($data);
        if($res){
            //添加操作记录
            $data = [];
            $data['sale_user_id'] = $input['sale_user_id'];
            $data['user_id'] = $this->users['id'];
            $data['content'] = '分销商'.$sale_user['faname'].'审核拒绝';
            $data['remark'] = '';
            SaleLog::addLog($data);

            $saleUserLogin = SaleUserLogin::GetSaleUser(array('sale_user_id' => $input['sale_user_id']));
            //发短信
//            ShopSms::saleRegisterRefuse($saleUserLogin['username'],$saleUserLogin['mobile']);

            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '审核拒绝执行成功', $data);
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'操作有误，请重新尝试');
        }
    }

    //查看申请审批/成员资料/个人中心资料
    public function saleApplyDetail(Request $request)
    {
        $input = $request->input();

        $sale_user_id = isset($input['sale_user_id'])?$input['sale_user_id']:$this->users['sale_user_id'];

        $data = SaleUser::GetSaleUser(array('id' => $sale_user_id));

        $data = formatPage($data);

        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
    }


    //成员管理
    public function saleUserList(Request $request)
    {
        $input = $request->input();
        //$this->users
        $where = [];
        //test
        $where[] = ['parent_user_id', $this->users['sale_user_id']];
        // $where[] = ['parent_user_id', 3];
        if (!empty($input['contact_tel'])) {
            $where[] = ['contact_tel', 'like', '%' . trim($input['contact_tel']) . '%'];
        }
        $page = $input['page'] ? $input['page'] : 1;
        $pageSize = $input['pageSize'] ? $input['pageSize'] : CommonService::PAGESIZE;
        $data = SaleUser::GetAllSaleUser($pageSize, $where);
        $data = formatPage($data);
        // dd($data);
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
    }

    //编辑成员资料
    public function saleUserEdit(Request $request)
    {
        $input = $request->input();
        $editData = array(
            'people' => $input['people']?$input['people']:$input['faname'], //联系人
            'contact_tel' => $input['contact_tel'], //联系人手机号
            'area_id' => $input['area_id'] ? $input['area_id']:0, //区域城市ID
            'address' => $input['address'] ? $input['address']:'', //公司/个人地址
            'companyid' => $input['companyid']?$input['companyid']:'', //工商/营业执照注册号
            'mechanism' => $input['mechanism']?$input['mechanism']:'', //公司/机构名称
            'faname' => $input['faname'], //法人/个人姓名
            'idcard' => $input['idcard'], //法人/个人身份证号
            'faidcard' => $input['faidcard'][0], //法人/个人身份证正面
            'faidcard_back' => $input['faidcard_back'][0], //法人/个人身份证反面(带国徽的一面)
            'shopcity' => $input['shopcity']?$input['shopcity']:'', //当前经营位置 省-市
            'update_time'=>date('Y-m-d H:i:s'), // 修改时间
            'email' => $input['email'], //邮箱
            'channel_name' => $input['channel_name'], //邮箱
        );
        $res = SaleUser::where('id',$input['sale_user_id'])->update($editData);
        if($res){
            //添加操作记录
            $data = [];
            $data['sale_user_id'] = $input['sale_user_id'];
            $data['user_id'] = $this->users['id'];
            $data['content'] = '编辑分销商'.$input['faname'].'的资料';
            $data['remark'] = '';
            SaleLog::addLog($data);
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '编辑成功', $data);
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'操作有误，请重新尝试');
        }
    }


    //账号管理
    public function saleSonList(Request $request)
    {
        $input = $request->input();

        $where = [];
        //test
        $where[] = ['sale_user_id', $this->users['sale_user_id']]; //属于本分销商的
        // $where[] = ['sale_user_id', 3];
        // $where[] = ['role', CommonService::SALE_SON];  //子账号
        if (!empty($input['username'])) {
            $where[] = ['username', 'like', '%' . trim($input['username']) . '%'];
        }
        if (!empty($input['mobile'])) {
            $where[] = ['mobile', 'like', '%' . trim($input['mobile']) . '%'];
        }
        $page = $input['page'] ? $input['page'] : 1;
        $pageSize = $input['pageSize'] ? $input['pageSize'] : CommonService::PAGESIZE;
        $data = SaleUserLogin::GetSaleUsers($pageSize, $where);
        $data = formatPage($data);
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
    }

    //添加子账号
    public function saleSonAdd(Request $request)
    {
        $input = $request->input();

        $userData = array(
                'sale_user_id'=>$this->users['sale_user_id'],
                'username'=>$input['username'], //用户名/昵称
                'password'=>md5($input['mobile'].'txz'.$input['mobile']), // 密码
                'mobile'=>$input['mobile'], //手机号码
                'is_show'=>CommonService::SHOP_ENABLE, //1启用
                'role'=>CommonService::SALE_SON,//角色 2子账号
            );
        $res = SaleUserLogin::insertGetId($userData);
        if($res){
            //添加操作记录
            $data = [];
            $data['sale_user_id'] = $this->users['sale_user_id'];
            $data['user_id'] = $this->users['id'];
            $data['content'] = '添加分销商的子账号'.$res;
            $data['remark'] = $input['username'].'--'.$input['mobile'];
            SaleLog::addLog($data);
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '添加子账号成功', $data);
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'操作有误，请重新尝试');
        }
    }

}
